import scopt.test.common
from scopt.mpu.security.db_registry.handlers import flyby_token
from scopt.mpu.security.db_registry.handlers import authenticate_login_password
from scopt.test.common import DEFAULT_LOGIN
from scopt.test.common import DEFAULT_PASSWORD


class FlybyToken(scopt.test.common.ScoptTest):
    '''Flyby token tests
    MPU should:

    1. test_valid_token
        send dummy request with valid token
    2. test_invalid_token
        send dummy request with invalid token
    '''
    def test_valid_token(self):
        handler = authenticate_login_password.handle
        token = handler.TOKEN()
        token['login'] = DEFAULT_LOGIN
        token['password'] = DEFAULT_PASSWORD
        response = self.send_request(self.dummy_request(), token)

        handler = flyby_token.handle
        token = handler.TOKEN()
        token['value'] = response['resd']['token']

        response = self.send_request(self.dummy_request(), token)

        self.assertEquals(response['resh']['status'], 'OK')
        self.assertIsNotNone(response['resd'])

    def test_invalid_token(self):
        handler = authenticate_login_password.handle
        token = handler.TOKEN()
        token['login'] = DEFAULT_LOGIN
        token['password'] = DEFAULT_PASSWORD
        self.send_request(self.dummy_request(), token)

        handler = flyby_token.handle
        token = handler.TOKEN()
        token['value'] = 'invalid_token'

        response = self.send_request(self.dummy_request(),
                                     token,
                                     pass_interrupts=True)

        self.assertEquals(response['resh']['status'], 'INT')
        self.assertEquals(response['resd']['reason'], 'EXCEPTION')
        self.assertEquals(response['resd']['code'], 'security.invalid.token')
        self.assertIsNotNone(response['resd']['text'])
